package com.google.coffeeshop.gui.desktop.receiptnote;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import vn.bang.utility.DateUtil;
import vn.bang.utility.LongUtil;
import vn.bang.utility.StringUtil;

import com.google.coffeeshop.entity.ReceiptNoteDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class ReceiptNoteTable extends AbstractBaseTable {
	private static final long serialVersionUID = 1L;
	
	private static String COL_RN_ID = "Receipt Note ID";
	private static String COL_CODE = "Code";
	private static String COL_AMOUNT = "Amount";
	private static String COL_CREATED_DATE = "Created Date";
	
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_RN_ID, COL_CODE, COL_AMOUNT, COL_CREATED_DATE,
		};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public ReceiptNoteTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	private void addNewRow(Long receiptNoteId, String code, String amount, String createdDate) {
		Object[] objects = new Object[] {receiptNoteId, code, amount, createdDate};
		model.addRow(objects);
	}

	public void addNewRow(ReceiptNoteDTO receiptNote) {
		addNewRow(
				receiptNote.getId(),
				receiptNote.getCode(), 
				String.valueOf(receiptNote.getAmount()),
				DateUtil.toString(receiptNote.getReceiptDate())
		);
	}
	
	public void addNewRows(List<ReceiptNoteDTO> receiptNotes) {
		for (ReceiptNoteDTO employee : receiptNotes) {
			addNewRow(employee);
		}
	}
	
	public ReceiptNoteDTO getReceiptNoteSelected() {
		int rowIndex = this.getSelectedRow();
		ReceiptNoteDTO receiptNote = new ReceiptNoteDTO();
		receiptNote.setId(
				(long)findModelValue(model, COL_RN_ID, rowIndex));
		receiptNote.setCode(
				(String)findModelValue(model, COL_CODE, rowIndex));
		receiptNote.setAmount(
				LongUtil.parseLong((String)findModelValue(model, COL_AMOUNT, rowIndex)));
		receiptNote.setReceiptDate(
				DateUtil.toDate(
						(String)findModelValue(model, COL_CREATED_DATE, rowIndex)));
		return receiptNote;
	}
	
	public List<ReceiptNoteDTO> getReceiptNoteList() {
		List<ReceiptNoteDTO> receiptNotes = new ArrayList<ReceiptNoteDTO>(); 
		for (int rowIndex = 1; rowIndex < model.getRowCount(); rowIndex++) {
			ReceiptNoteDTO receiptNote = getReceiptNoteSelected();
			receiptNotes.add(receiptNote);
		}
		return null;
	}
	
}
